﻿#include "epibolyview.h"
#include "ui_epibolyview.h"

EpibolyView::EpibolyView(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::EpibolyView)
{
    ui->setupUi(this);

    model=new QSqlQueryModel(this);
    stuffModel=new QSqlQueryModel(this);
    routineModel=new QSqlQueryModel(this);

    ui->tableView->setModel(model);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);

    connect(ui->tableView,SIGNAL(clicked(QModelIndex)),this,SLOT(updateStuff(QModelIndex)));
    connect(ui->tableView,SIGNAL(clicked(QModelIndex)),this,SLOT(updateRoutine(QModelIndex)));
    connect(ui->tableView,SIGNAL(clicked(QModelIndex)),this,SLOT(updateDetail(QModelIndex)));
    connect(ui->tableView,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(showRoutine(QModelIndex)));

    ui->stuffView->setModel(stuffModel);
    ui->stuffView->setSelectionBehavior(QAbstractItemView::SelectRows);

    ui->routineView->setModel(routineModel);
    ui->routineView->setSelectionBehavior(QAbstractItemView::SelectRows);

    setQuery();
}

EpibolyView::~EpibolyView()
{
    delete ui;
}

void EpibolyView::setQuery()
{
    QSqlQuery query("select date,batch,contractor,remark from td_epiboly where done is NULL");
    model->setQuery(query);

    model->setHeaderData(0,Qt::Horizontal,QStringLiteral("日期"));
    model->setHeaderData(1,Qt::Horizontal,QStringLiteral("批次"));
    model->setHeaderData(2,Qt::Horizontal,QStringLiteral("承包商"));
    model->setHeaderData(3,Qt::Horizontal,QStringLiteral("备注"));
}

void EpibolyView::updateStuff(const QModelIndex &index)
{
    if(index.isValid())
    {
        QSqlRecord record=model->record(index.row());
        QString batch=record.value("batch").toString();

        QSqlQuery query(QString("select name,number,remark from td_epiboly_stuff where batch='%1'").arg(batch));
        stuffModel->setQuery(query);

        stuffModel->setHeaderData(0,Qt::Horizontal,QStringLiteral("名称"));
        stuffModel->setHeaderData(1,Qt::Horizontal,QStringLiteral("数量"));
        stuffModel->setHeaderData(2,Qt::Horizontal,QStringLiteral("备注"));
    }
}

void EpibolyView::updateRoutine(const QModelIndex &index)
{
    if(index.isValid())
    {
        QSqlRecord record=model->record(index.row());
        QString batch=record.value("batch").toString();

        QSqlQuery query(QString("select date,name,number,remark from td_epiboly_routine where batch='%1'").arg(batch));
        routineModel->setQuery(query);

        routineModel->setHeaderData(0,Qt::Horizontal,QStringLiteral("日期"));
        routineModel->setHeaderData(1,Qt::Horizontal,QStringLiteral("名称"));
        routineModel->setHeaderData(2,Qt::Horizontal,QStringLiteral("数量"));
        routineModel->setHeaderData(3,Qt::Horizontal,QStringLiteral("备注"));
    }
}

void EpibolyView::updateRoutine(const QString &batch)
{
    QSqlQuery query(QString("select date,name,number,remark from td_epiboly_routine where batch='%1'").arg(batch));
    routineModel->setQuery(query);

    routineModel->setHeaderData(0,Qt::Horizontal,QStringLiteral("日期"));
    routineModel->setHeaderData(1,Qt::Horizontal,QStringLiteral("名称"));
    routineModel->setHeaderData(2,Qt::Horizontal,QStringLiteral("数量"));
    routineModel->setHeaderData(3,Qt::Horizontal,QStringLiteral("备注"));
}

void EpibolyView::showRoutine(const QModelIndex &index)
{
    if(index.isValid())
    {
        QSqlRecord record=model->record(index.row());
        QString batch=record.value("batch").toString();

        EpibolyRoutine routine(this,batch);
        if(routine.exec()==QDialog::Accepted)
            updateRoutine(batch);
    }
}

void EpibolyView::updateDetail(const QModelIndex &index)
{
    if(index.isValid())
    {
        QSqlRecord record=model->record(index.row());
        QString batch=record.value("batch").toString();

        QSqlQuery query(QString("select job from td_epiboly where batch='%1'").arg(batch));
        query.exec();
        if(query.next())
        {
            QString content=query.value(0).toString();
            ui->detail->setText(content);
        }
    }
}
